package cn.com.nxt.txb.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cn.com.nxt.txb.R;
import cn.com.nxt.txb.model.BusinessNumber;
import cn.com.nxt.txb.util.OnRecyclerItemClickListener;

public class InviteFriendsAdapter extends RecyclerView.Adapter<InviteFriendsAdapter.MyViewHolder> {

    public InviteFriendsAdapter(Context mContext, List<BusinessNumber> mDatas) {
        super();
        this.mContext = mContext;
        this.mDatas = mDatas;
    }

    private Context mContext;
    protected List<BusinessNumber> mDatas;
    private SparseBooleanArray mSelectedPositions = new SparseBooleanArray();
    private boolean mIsSelectable = false;

    private OnRecyclerItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnRecyclerItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    //更新adpter的数据和选择状态
    public void updateDataSet(ArrayList<BusinessNumber> list) {
        this.mDatas = list;
        mSelectedPositions = new SparseBooleanArray();
        notifyDataSetChanged();
    }

    //获得选中条目的结果
    public ArrayList<BusinessNumber> getSelectedItem() {
        ArrayList<BusinessNumber> selectList = new ArrayList<>();
        for (int i = 0; i < mDatas.size(); i++) {
            if (isItemChecked(i)) {
                selectList.add(mDatas.get(i));
            }
        }
        return selectList;
    }

    //设置给定位置条目的选择状态
    private void setItemChecked(int position, boolean isChecked) {
        mSelectedPositions.put(position, isChecked);
    }

    //根据位置判断条目是否选中
    private boolean isItemChecked(int position) {
        return mSelectedPositions.get(position);
    }

    //根据位置判断条目是否可选
    private boolean isSelectable() {
        return mIsSelectable;
    }

    //设置给定位置条目的可选与否的状态
    private void setSelectable(boolean selectable) {
        mIsSelectable = selectable;
    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder holder = new MyViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_invite_friends, parent, false));
        return holder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        BusinessNumber record = mDatas.get(position);
        holder.tvName.setText(record.getName());
        if (isItemChecked(position)) {
            holder.cb.setChecked(true);
        } else {
            holder.cb.setChecked(false);
        }
        //holder.tvphone.setText(record.getPhone());
        //holder.tvTime.setText(record.getTime());
        // 绑定每一个itemView的事件  抽取方法 供子类复用
        //setItemViewEvent(holder);
        //checkBox的监听
        ((MyViewHolder) holder).cb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isItemChecked(position)) {
                    setItemChecked(position, false);
                } else {
                    setItemChecked(position, true);
                }
                mOnItemClickListener.OnItemClick(holder.itemView, position);
            }
        });
    }

    protected void setItemViewEvent(final MyViewHolder holder) {
        if (mOnItemClickListener != null) {
            holder.itemView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    int realPos = holder.getLayoutPosition();
                    mOnItemClickListener.OnItemClick(holder.itemView, realPos);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    void louisDelete(int pos) {
        mDatas.remove(pos);
        this.notifyItemRemoved(pos);
    }

    class MyViewHolder extends ViewHolder {
        CheckBox cb;
        TextView tvName;
        TextView tvTime;

        public MyViewHolder(View view) {
            super(view);
            cb = (CheckBox) view.findViewById(R.id.cb);
            tvName = (TextView) view.findViewById(R.id.tv_name);
            tvTime = (TextView) view.findViewById(R.id.tv_time);
        }
    }
}
